package com.shopping.swagger.config;

/**
 * 自动加载bean的配置对象
 *
 * @author: zlw
 * @date: 2024/6/30
 */

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestBody;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnProperty(prefix = "swagger",name = "enabled")
public class SwaggerAutoConfiguration {

    @Bean
    public Docket userDocket(SwaggerProperties properties){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2. 用户应用分组接口")
//                .ignoredParameterTypes(RequestBody.class)
                .apiInfo(apiInfo(properties))
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getServiceBasePackage() + ".controller.user"))
                .paths(PathSelectors.any()).build();
    }

    @Bean
    public Docket simpleDocket(SwaggerProperties properties){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("2. 示例应用分组接口")
//                .ignoredParameterTypes(RequestBody.class)
                .apiInfo(apiInfo(properties))
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getServiceBasePackage() + ".controller.simple"))
                .paths(PathSelectors.any()).build();
    }
    private ApiInfo apiInfo(SwaggerProperties properties){
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .version(properties.getVersion()).build();
    }
}
